import Vue from 'vue'
import Vuex from 'vuex'
import { getStore,setStore } from '@/util/store';

//把Veux作为插件 引入到vue全局实例中
Vue.use(Vuex)

//创建仓库
export default new Vuex.Store({
	//全局状态
	state:{
		ServiceIndex:0,//dervice页面项目索引
		access_token:getStore({
      name: 'access_token'
    }) || '',
		refresh_token: getStore({
		  name: 'refresh_token'
		}) || ''
	},
	//修改状态
	mutations:{
		proIndex(state, data){
			state.ServiceIndex = data;
		}
	},
	//异步修改状态
	actions:{
		// 根据用户名登录
		LoginByUsername({ commit }, userInfo) {
		  const user = encryption({
		    data: userInfo,
		    key: 'thanks,pig4cloud',
		    param: ['password']
		  })
		  return new Promise((resolve, reject) => {
		    loginByUsername(user.username, user.password, user.code, user.randomStr,user.smsCode,user.isLoginSms).then(response => {
		      const data = response.data
		      commit('SET_ACCESS_TOKEN', data.access_token)
		      commit('SET_REFRESH_TOKEN', data.refresh_token)
		      commit('CLEAR_LOCK')
		      resolve()
		    }).catch(error => {
		      reject(error)
		    })
		  })
		},
	},
	getters:{
		getType(state){
			return state.ServiceIndex
		}
	}
})